/**
 * Created by claim on 16-10-23.
 */
const db = require('./../../model/index');
const helper = require('./../../helper/auth');
const responser = require('./../../lib/responser');
const cache = require('./../../instance/cache');
const Check = require("../../lib/check.js").Check

const User = db.models.User

module.exports = router=> {
    router.post("/user/register", async (ctx) => {
        Check(ctx,"username","email","password","verifyCode")

        var body = ctx.request.body
        var email = body.email
        var password = body.password
        var username = body.username
        var verifyCode = body.verifyCode

        // check user email
        var user = await User.findOne({
            where : {
                email
            }
        })
        if(user){
            responser.reject(ctx,"emailRepeat")
            return
        }

        // check user username
        user = await User.findOne({
            where : {
                username
            }
        })
        if(user){
            responser.reject(ctx,"usernameRepeat")
            return
        }

        // check code
        var code = await cache.get(email)
        if(typeof code == "undefined" || code == null || code !== verifyCode){
            responser.reject(ctx,"verifyCodeError")
            return
        }

        // create user
        var error
        user = await User.create({
            email,
            password,
            username,
            avatar: '/defaultAvatar.jpg',
            type: body.type === 'OurEDA_admin' ? 1 : 0
        }).catch(err => {
            error = err
        })
        if(error){
            responser.reject(ctx,"userCreateError")
            return
        }

        // success
        let LoginToken = helper.login(ctx, user);
        responser.success(ctx, {
            LoginToken
        });
    })


    // todo : email 不能空 此接口停掉
    // router.post('/user/add', async(ctx, next)=> {
    //     let body = ctx.request.body;
    //     let error;
    //     let user = await db.models.User.create({
    //         username: body.username,
    //         password: body.password,
    //         email: body.email || 'NULL',
    //         mobile: body.mobile || 'NULL',
    //         avatar: '', //todo : get update avatar
    //         type: body.type === 'OurEDA_admin' ? 1 : 0
    //     }).catch(err=> {
    //         error = err;
    //     });
    //
    //     if (user) {
    //         let LoginToken = helper.login(ctx, user);
    //         responser.success(ctx, {
    //             LoginToken: LoginToken
    //         });
    //     } else {
    //         let errorInfo;
    //         if (error && /SequelizeUniqueConstraintError/.test(error.toString())) {
    //             errorInfo = 'Username has been used';
    //         }
    //         if (errorInfo)
    //             responser.reject(ctx, errorInfo, 403);
    //         else
    //             responser.catchErr(ctx, error, 500);
    //     }
    //     await next();
    // });
};